define(['jquery', 'bootstrap', 'backend', 'table', 'form','vue'], function ($, undefined, Backend, Table, Form,Vue) {

    return {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xykeep/course/index' + location.search,
                    add_url: 'xykeep/course/add',
                    edit_url: 'xykeep/course/edit',
                    del_url: 'xykeep/course/del',
                    multi_url: 'xykeep/course/multi',
                    import_url: 'xykeep/course/import',
                    table: 'xykeep_course',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'image',
                            title: __('Image'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'price', title: __('Price'), operate: 'BETWEEN'},
                        {field: 'duration', title: __('Duration')},
                        {field: 'sales', title: __('Sales'), sortable: true},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {"normal": __('Normal'), "hidden": __('Hidden')},
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            //修改默认弹窗大小
            Fast.config.openArea = ['90%', '90%'];

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'xykeep/course/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'image',
                            title: __('Image'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'price', title: __('Price'), operate: 'BETWEEN'},
                        {field: 'duration', title: __('Duration')},
                        {field: 'start_sale', title: __('Start_sale')},
                        {field: 'sales', title: __('Sales'), sortable: true},
                        {field: 'views', title: __('Views'), sortable: true},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'xykeep/course/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'xykeep/course/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            var courseDetail = new Vue({
                el: "#courseDetail",
                data() {
                    return {
                        is_dis: 1,//是否参与分销
                        dis_rule: 0,//分佣方式
                        commissionRule: [],//佣金规则
                    }
                },
                mounted() {
                    this.getInit(Config.disLevel);
                },
                methods: {
                    getInit(disLevel) {
                        var commissionRule = [];
                        disLevel.forEach((item, index) => {
                            commissionRule.push({
                                level_id: item.id,
                                level_name: item.name,
                                commission_one: 0.00,
                                commission_two: 0.00
                            });
                        });
                        this.commissionRule = commissionRule;
                    },
                    //是否分销
                    changeDis(value) {
                        this.is_dis = value;
                    },
                    //切换分销
                    changeRule(value) {
                        this.dis_rule = value;
                    },
                },

            })

            Form.api.bindevent($("form[role=form]"), function (data, ret) {
                //console.log(data,ret);
            }, function (data, ret) {
                //console.log(data,ret);
            }, function (success, error) {
                //提交之前设置数据
                $("#commission_rule").val(JSON.stringify(courseDetail.commissionRule));
            });
        },
        edit: function () {
            var courseDetail = new Vue({
                el: "#courseDetail",
                data() {
                    return {
                        is_dis: Config.is_dis,//是否参与分销
                        dis_rule: Config.dis_rule,//分佣方式
                        commissionRule: [],//佣金规则
                    }
                },
                mounted() {
                    this.getInit(Config.commission_rule, Config.disLevel);
                },
                methods: {
                    getInit(commissionRule, disLevel) {
                        var commissionRule = commissionRule ? JSON.parse(commissionRule) : [];
                        disLevel.forEach((item, index) => {
                            var flag = false;
                            commissionRule.forEach((item1, index1) => {
                                if (item.id == item1.level_id) {
                                    flag = true;
                                }
                            });
                            if (!flag) {
                                commissionRule.push({
                                    level_id: item.id,
                                    level_name: item.name,
                                    commission_one: 0.00,
                                    commission_two: 0.00
                                });
                            }
                        });
                        this.commissionRule = commissionRule;
                    },
                    //是否分销
                    changeDis(value) {
                        this.is_dis = value;
                    },
                    //切换
                    changeRule(value) {
                        this.dis_rule = value;
                    },
                },

            })

            Form.api.bindevent($("form[role=form]"), function (data, ret) {
                //console.log(data,ret);
            }, function (data, ret) {
                //console.log(data,ret);
            }, function (success, error) {
                //提交之前设置数据
                $("#commission_rule").val(JSON.stringify(courseDetail.commissionRule));
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
});
